'use strict';
const DomTouch = function(dom) {
	this.dom = dom;
	const _this = this;

	//触摸场景--手机端/触摸屏机
	this.dom.addEventListener("touchstart", this.downEvent.bind(this), false);
	this.dom.addEventListener("touchmove", this.moveEvent.bind(this), false);
	this.dom.addEventListener("touchend", this.upEvent.bind(this), false);
	this.dom.addEventListener("contextmenu", function(e) {
		e.preventDefault()
	}, false);

	//鼠标场景--电脑端.
	this.dom.onmousedown = this.downEvent.bind(this);
	this.dom.onmousemove = this.moveEvent.bind(this);
	this.dom.onmouseup = this.upEvent.bind(this);
	this.dom.onmouseleave = this.upEvent.bind(this);
	this.moveFlag = false;
	this.p = 0;
	this.arr = [];
	this.upof = {};
	this.radius = 0;
	this.has = [];

	// document.getElementById('pen2'); //默认笔刷
	//this.img1 = document.getElementById('pen2'); 
	window.addEventListener("resize", this.resize.bind(this), false);

	this.resize();
}

DomTouch.prototype.clickEvent = function(e) {
	this.cli = this.getXY(e);
}
DomTouch.prototype.downEvent = function(e) {
	window.console.log('点击');
	this.moveFlag = true;
	this.has = [];
	this.upof = this.getXY(e);

};
DomTouch.prototype.upEvent = function(e) {
	this.moveFlag = false;
}
DomTouch.prototype.moveEvent = function(e) {
	if (!this.moveFlag)
		return;
	e.preventDefault();
	var ofp = this.getXY(e); //move 
	var up = this.upof; //down


	this.onMove();
	this.upof = ofp;
}
DomTouch.prototype.onMove = function() {

}
DomTouch.prototype.getXY = function(e) {
	var x = 0,
		y = 0;
	var teo = e.touches;
	if (teo) {
		teo = teo[0];
		x = teo.offsetX || teo.clientX;
		y = teo.offsetY || teo.clientY;

	} else {
		x = e.offsetX;
		y = e.offsetY;
	}
	if (e.currentTarget) {
		var rect = e.currentTarget.getBoundingClientRect();
		x = x - rect.x;
		y = y - rect.y;
	}
	return {
		x: x,
		y: y
	}
}
DomTouch.prototype.distance = function(a, b) {
	let x = b.x - a.x,
		y = b.y - a.y;
	return Math.sqrt(x * x + y * y);
}
